
%%% The Ambiguity of Fishing for Fun, October 2022
%%%This function evaluates the difference between the actual and the expected keep for a given
%%%minimum size limit

function[delta]=pstar(M,N,SFreshaffle,p,actual,baglimit)

keep=zeros(1,N);
kept=zeros(1,M);

for j=1:M
catchPerTrip=SFreshaffle(j,:);
for i=1:N
 if catchPerTrip(i)>0 k=rand(1,catchPerTrip(i));
 k=(k>=p);
 keep(i)=min(baglimit,sum(k,'all'));
 else
 keep(i)=0;  
 end 
end
kept(j)=mean(keep);
end

delta=actual-mean(kept);
end